//
//  UIBarButtonItem+Extension.m
//  DSJY
//
//  Created by 冯华强 on 15/9/8.
//  Copyright (c) 2015年 HQ. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"
#import "UIView+Extension.h"
#import "NSString+Extension.h"

@implementation UIBarButtonItem (Extension)

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image title:(NSString *)title
{
    return [self itemWithTarget:target action:action image:image
                          title:title selectImage:nil selectTitle:nil titleColor:[UIColor whiteColor]];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image title:(NSString *)title titleColor:(UIColor *)titleColor
{
    return [self itemWithTarget:target action:action image:image
                          title:title selectImage:nil selectTitle:nil titleColor:titleColor];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image title:(NSString *)title selectImage:(NSString *)selectImage selectTitle:(NSString *)selectTitle titleColor:(UIColor *)titleColor
{
    //UIButtonTypeCustom的类型要设置frame才能显示
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage *normalImage = [UIImage imageNamed:image];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateHighlighted];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:selectTitle forState:UIControlStateHighlighted];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    CGFloat leftMargin = 8;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, leftMargin, 0, 0);
    //因为button都是继承UIView的，所有使用类别出一些公共的方法使用
    
    leftMargin = [title sizeWithFont:btn.titleLabel.font].width == 0 ? 16 : leftMargin;
    btn.frame = CGRectMake(0, 0, [title sizeWithFont:btn.titleLabel.font].width + normalImage.size.width + leftMargin, 44);
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)itemMapWithTarget:(id)target action:(SEL)action image:(NSString *)image selectImage:(NSString *)selectImage
{
    //UIButtonTypeCustom的类型要设置frame才能显示
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:selectImage] forState:UIControlStateHighlighted];
    btn.contentMode = UIViewContentModeLeft;
    //    [btn setTitle:title forState:UIControlStateNormal];
    //    [btn setTitle:selectTitle forState:UIControlStateHighlighted];
    //因为button都是继承UIView的，所有使用类别出一些公共的方法使用
    btn.size = btn.currentBackgroundImage.size;
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

//空白间隙的Item
+ (UIBarButtonItem *)itemNegativeSpacer
{
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -11;
    return negativeSpacer;
}

//创建按钮
+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [title sizeWithFont:font].width, 30)];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = font;
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

@end
